package com.yc.boot.train.business;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.boot.common.model.response.Outcome;
import com.yc.boot.sys.business.SysUserBusiness;
import com.yc.boot.sys.entity.SysUser;
import com.yc.boot.sys.model.UserInfo;
import com.yc.boot.sys.service.SysUserService;
import com.yc.boot.train.entity.GradeCommentFeedback;
import com.yc.boot.train.entity.Notice;
import com.yc.boot.train.model.find.IPageNotice;
import com.yc.boot.train.model.find.NoticeMessage;
import com.yc.boot.train.model.save.SaveNoticeManage;
import com.yc.boot.train.model.update.UpdataGradeManage;
import com.yc.boot.train.model.update.UpdateNotice;
import com.yc.boot.train.service.AddNoticeService;
import com.yc.boot.train.service.NoticeService;
import com.yc.boot.train.service.impl.NoticeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Create with IntelliT IDEA
 *
 * @Author: zhengmingzhe
 * @Date: 2022/01/11/14:12
 * @Description:
 */
@Slf4j
@Service
public class NoticeManageBussiness {
    @Resource
    private NoticeService  noticeService;

    @Resource
    private SysUserBusiness sysUserBusiness;

    @Resource
    private AddNoticeService addNoticeService;

    @Resource
    private SysUserService sysUserService;
    /*
     * @Description  分页查询通知公告信息
     * @Param
     * @return
     **/
    public IPage<NoticeMessage> queryNoticeMessage(IPageNotice param){
//        UserInfo user = sysUserBusiness.getUserInfo();
        UserInfo user =new UserInfo();
        user.setId(3L);
        IPage<NoticeMessage> noticeMessage =noticeService.queryNoticeMessage(new Page<>(param.getCurrent(), param.getSize()),
                new QueryWrapper<NoticeMessage>().lambda()
//                        .eq(ObjectUtil.isNotEmpty(user.getId()),NoticeMessage::getCreateBy,user.getId())
                        .like(ObjectUtil.isNotEmpty(param.getNoticeName()),NoticeMessage::getNoticeName,param.getNoticeName())
                        .eq(ObjectUtil.isNotEmpty(param.getId()),NoticeMessage::getId,param.getId())
                        .eq(NoticeMessage::getIsDelete,0)
        );
        return noticeMessage;
    }

    /*
     * @Description 根据id查询公告信息
     * @Param
     * @return
     **/
    public NoticeMessage QueryNoticeById(NoticeMessage param){
        NoticeMessage notice =Convert.convert(NoticeMessage.class,addNoticeService.getById(param.getId()));
        SysUser user =sysUserService.getOne(new QueryWrapper<SysUser>().lambda().eq(ObjectUtil.isNotEmpty(notice.getCreateBy()),SysUser::getId,notice.getCreateBy()));
        notice.setCreatePerson(user.getUserName());
        return notice;
    }
    /*
     * @Description   添加公告信息
     * @Param
     * @return
     **/
    public Outcome saveNoticeManage(SaveNoticeManage param){
        UserInfo login = sysUserBusiness.getUserInfo();
//        UserInfo login=new UserInfo();
//            login.setId(3L);
        Notice notice = Convert.convert(Notice.class,param);
        notice.setCreateBy(login.getId());
        Boolean save =addNoticeService.save(notice);
        return Outcome.status(save);
    }
    /*
     * @Description  修改公告信息
     * @Param
     * @return
     **/
    public Outcome updateNoticeManage(UpdateNotice param){
        UserInfo login = sysUserBusiness.getUserInfo();
//        UserInfo login =new UserInfo();
//        login.setId(3L);
        Notice notice = Convert.convert(Notice.class,param);
        notice.setUpdateBy(login.getId());
        notice.setCreateBy(login.getId());
        Boolean update = addNoticeService.updateById(notice);
        return Outcome.status(update);
    }

    /*
     * @Description 删除公告信息
     * @Param
     * @return
     **/
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> id) {
//          UserInfo userInfo = sysUserBusiness.getUserInfo();
        UserInfo userInfo =new UserInfo();
        userInfo.setId(3L);
        Boolean updateById= addNoticeService.updateBatchById(Convert.toList(Notice.class,id.stream().map(n->new Notice().setUpdateBy(userInfo.getId()).setId(n)).collect(Collectors.toList())));
        Boolean removeById =addNoticeService.removeByIds(id);
        return updateById&&removeById;
    }
}
